package com.luxuewen.sxa.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.luxuewen.sxa.common.annotation.ApiAuth;
import com.luxuewen.sxa.entity.SxaMenus;
import com.luxuewen.sxa.entity.validator.BatchDeleteVo;
import com.luxuewen.sxa.service.impl.SxaMenusServiceImpl;
import com.luxuewen.sxa.utils.response.ApiResponse;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author wen
 */
@RestController
@Slf4j
@Validated
public class MenuController {
    @Autowired
    private SxaMenusServiceImpl sxaMenusService;

    @ApiAuth(alias = "menu:create")
    @PostMapping("menu/create")
    public ApiResponse<Integer> create (@Validated @RequestBody SxaMenus sxaMenus) {
        QueryWrapper<SxaMenus> sxaMenusQueryWrapper = new QueryWrapper<>();
        sxaMenusQueryWrapper.eq("name", sxaMenus.getName());
        boolean ex = sxaMenusService.exists(sxaMenusQueryWrapper);
        if (ex) {
            return ApiResponse.fail("菜单别名已存在，请更换后再试！");
        }

        boolean res = sxaMenusService.save(sxaMenus);
        if (!res) {
            return ApiResponse.fail("菜单添加失败！");
        }

        return ApiResponse.success(sxaMenus.getId());
    }

    @ApiAuth(alias = "menu:update")
    @PostMapping("menu/update")
    public ApiResponse<Boolean> update (@Validated @RequestBody SxaMenus sxaMenus) {
        return ApiResponse.success(sxaMenusService.update(sxaMenus));
    }

    @ApiAuth(alias = "menu:delete")
    @PostMapping("menu/delete/{id}")
    public ApiResponse<SxaMenus> delete (@NotNull @PathVariable Integer id) {
        boolean ex = sxaMenusService.removeById(id);
        if (!ex) {
            return ApiResponse.fail("删除失败，请刷新后再试！");
        }

        return ApiResponse.success();
    }

    @ApiAuth(alias = "menu:batch:update")
    @PostMapping("menu/batch/delete")
    public ApiResponse<SxaMenus> delete (@NotNull @RequestBody BatchDeleteVo batchDeleteVo) {
        boolean ex = sxaMenusService.removeBatchByIds(batchDeleteVo.getIds());
        if (!ex) {
            return ApiResponse.fail("删除失败，请刷新后再试！");
        }

        return ApiResponse.success();
    }

    @ApiAuth(alias = "menu:list")
    @GetMapping("menu/list")
    public ApiResponse<List<Map<String, Object>>> list () {
        return ApiResponse.success(sxaMenusService.treeWrapper());
    }

    @GetMapping("menu/my/list")
    public ApiResponse<List<Map<String, Object>>> myList () {
        return ApiResponse.success(sxaMenusService.myList());
    }

    @GetMapping("menu/my/btn")
    public ApiResponse<List<String>> myBtn () {
        return ApiResponse.success(sxaMenusService.myButtonAuth());
    }

    @GetMapping("menu/my/ids/{roleId}")
    public ApiResponse<List<Integer>> myRoleList (@PathVariable Integer roleId) {
        return ApiResponse.success(sxaMenusService.myRoleList(roleId));
    }

    @GetMapping("menu/detail/{id}")
    public ApiResponse<SxaMenus> detail (@NotNull @PathVariable Integer id) {
        SxaMenus detail = sxaMenusService.getById(id);
        return ApiResponse.success(detail);
    }
}
